/**
 * @Author your name
 * @Date 2021-03-05 09:51:32
 * @LastEditTime 2021-03-05 15:00:55
 * @LastEditors Please set LastEditors
 * @Description In User Settings Edit
 * @FilePath \fed-e-task-04-06\code\angular-demo\src\app\store\index.ts
 */
// src\app\store\index.ts
import {
  ActionReducer,
  ActionReducerMap,
  createFeatureSelector,
  createSelector,
  MetaReducer
} from '@ngrx/store';
import { environment } from '../../environments/environment';
import * as fromCounte from './reducers/counter.reducer';
import * as fromTodo from './reducers/todo.reducer';
import * as fromRouter from '@ngrx/router-store';

// store 中存储的状态类型接口
export interface AppState {
  // 添加 router 的数据类型
  router: fromRouter.RouterReducerState;
  [fromCounte.counteFeatureKey]: fromCounte.State;
  [fromTodo.todoFeatureKey]: fromTodo.State;
}

// 状态名字和 reducer 的对应关系
export const reducers: ActionReducerMap<AppState> = {
  // 设置 router 路由状态
  router: fromRouter.routerReducer,
  [fromCounte.counteFeatureKey]: fromCounte.reducer,
  [fromTodo.todoFeatureKey]: fromTodo.reducer,
};

function logger (reducer: ActionReducer<AppState>): ActionReducer<AppState> {
  return function (state, action) {
    let result = reducer(state, action)
    // console.log("最新的状态", result)
    // console.log("上次的状态", state)
    // console.log("action", action)
    return result
  }
}

export const metaReducers: MetaReducer<AppState>[] = !environment.production 
  ? [logger] 
  : [];
